import { Component,Injectable} from '@angular/core';
import { ScoreService} from './score.service';
import { PopService} from 'dolphinng';
import { Score} from '../../services/model/score';
import { Course} from '../../services/model/course';
@Component({
    selector: 'score',
    templateUrl: './score.component.html',
    styleUrls: ['./score.component.less'],
    providers:[ScoreService,PopService]
})
@Injectable()
export class ScoreComponent {

  tableData:any[];
  page:{
    pageIndex:number,
    pageSize:number,
    count:number
  };
  searchParams:{
    studentId?:number,
    studentName?:string,
    majorId?:number,
    gradeId?:number,
    courseId?:number,
    min?:number,
    max?:number
  };

  courses:Course[];
  majorOptions:any;
  gradeOptions:any;
  courseOptions:any;



  modalAdd={
    visible:false,
    submitted:false,
    data:new Score(),
    courseOptions:[]
  };
  modalEdit={
    visible:false,
    submitted:false,
    data:new Score(),
    courseOptions:[]
  };
  constructor(private scoreSvc:ScoreService,private pop:PopService){
    this.resetParams();
    this.getGrades();
    this.getMajors();
    this.getCourses();
    this.page={
      pageIndex:0,
      pageSize:10,
      count:0
    };
  }
  getGrades(){
    this.scoreSvc.getGrades()
      .then((data)=>{
        if(data.length) {
          this.gradeOptions = data;
          this.gradeOptions.unshift({gradeId: -1, name: '不限'});
        }
        console.log(this.gradeOptions);
      })
  }
  getMajors(){
    this.scoreSvc.getMajors()
      .then((data)=>{
        if(data.length) {
          this.majorOptions = data;
          this.majorOptions.unshift({majorId: -1, major: '不限'});
        }
        console.log(this.majorOptions);
      })
  }
  getCourses(){
    this.scoreSvc.getCourses()
      .then((data)=>{
        if(data.length) {
          this.courses = data;
          let course=new Course();
          course.courseId=-1;
          course.courseName='不限';
          this.courseOptions=[course].concat(this.courses);
        }
        console.log(this.courseOptions);
      })
  }
  search(){
    this.page.pageIndex=0;
    this.page.count=1;
    this.tableData=[];
    this.query();
  }
  query(){
    let params={
      studentId:this.searchParams.studentId,
      studentName:this.searchParams.studentName,
      majorId:this.searchParams.majorId,
      gradeId:this.searchParams.gradeId,
      courseId:this.searchParams.courseId,
      min:this.searchParams.min,
      max:this.searchParams.max,
      pageIndex:this.page.pageIndex,
      pageSize:this.page.pageSize
    };
    this.scoreSvc.query(params)
      .then((res)=>{
        console.log(res);
        this.tableData=res.items;
        this.page.count=res.count;
      });
  }

  resetParams(){
    this.searchParams={
      majorId:-1,
      gradeId:-1,
      courseId:-1,
    }
  }

  testScore(score:Score):boolean{
    let  regExp=/^[0-9]+$/;
    if(!score.studentId){
      this.pop.error({
        text:'请输入学号！'
      });
      return false;
    }else if(score.courseId==-1){
      this.pop.error({
        text:'请选择课程！'
      });
      return false;
    }else if(!score.score){
      this.pop.error({
        text:'请输入成绩！'
      });
      return false;
    }else if(!regExp.test(score.score+'')){
      this.pop.error({
        text:'成绩输入有误！'
      });
      return false;
    }
    return true;
  }
  showAddModal(){
    this.modalAdd.submitted=false;
    this.modalAdd.data=new Score();
    this.modalAdd.data.courseId=-1;
    let course=new Course();
    course.courseId=-1;
    course.courseName='请选择';
    this.modalAdd.courseOptions=[course].concat(this.courses);
    this.modalAdd.visible=true;
  }
  closeAddModal(){
    this.modalAdd.visible=false;
  }
  add(){
    if(this.testScore(this.modalAdd.data)){
      this.modalAdd.submitted=true;
      this.scoreSvc.add(this.modalAdd.data)
        .then((data)=>{
          this.modalAdd.submitted=false;
          this.modalAdd.visible=false;
          if(data.status){
            this.pop.info({
              text:'添加成功！'
            });
            this.query();
          }else{
            this.pop.info({
              title:'添加失败！',
              text:data.message
            });
          }
        });
    }
  }
  showEditModal(score:Score){
    this.modalEdit.submitted=false;
    this.modalEdit.data=new Score();
    let course=new Course();
    course.courseId=-1;
    course.courseName='请选择';
    this.modalEdit.courseOptions=[course].concat(this.courses);
    this.modalEdit.data.scoreId=score.scoreId;
    this.modalEdit.data.score=score.score;
    this.modalEdit.data.courseId=score.courseId;
    this.modalEdit.data.courseName=score.courseName;
    this.modalEdit.data.studentId=score.studentId;
    this.modalEdit.visible=true;
  }
  closeEditModal(){
    this.modalEdit.visible=false;
  }
  update(){
    if(this.testScore(this.modalEdit.data)){
      this.modalEdit.submitted=true;
      this.scoreSvc.update(this.modalEdit.data)
        .then((data)=>{
          this.modalEdit.submitted=false;
          this.modalEdit.visible=false;
          if(data.status){
            this.pop.info({
              text:'修改成功！'
            });
            this.query();
          }else{
            this.pop.info({
              title:'修改失败！',
              text:data.message
            });
          }
        });
    }
  }
  remove(id:number) {
    this.pop.confirm({
      text: '确定要删除这个成绩？',
      closeOnConfirm: false,
      showLoaderOnConfirm: true
    }).onConfirm(()=> {
      this.scoreSvc.remove(id)
        .then((data)=> {
          if (data.status) {
            this.pop.info({
              text: '删除成功！'
            });
            this.query();
          } else {
            this.pop.info({
              title: '删除失败！',
              text: data.message
            });
          }
        });
    })
  }

}
